package NLP;

import java.util.ArrayList;
import java.util.List;

import sentStruct.Adjective;
import sentStruct.Adverb;
import sentStruct.Noun;
import sentStruct.PartOfSpeech;
import sentStruct.Verb;
import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.trees.semgraph.SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation;
import edu.stanford.nlp.util.CoreMap;

public class SentenceElementsExtractor {
	
	CoreMap sentenceAnnotation;
	
	

	
	
	
public SentenceElementsExtractor(CoreMap sentenceAnnotation) {
		super();
		this.sentenceAnnotation = sentenceAnnotation;
	}

public CoreMap getSentenceAnnotation() {
		return sentenceAnnotation;
	}

	public void setSentenceAnnotation(CoreMap sentenceAnnotation) {
		this.sentenceAnnotation = sentenceAnnotation;
	}

/**
 * @param sentenceAnnotation
 * @return a list of subjects with relative ajdectives for each subject (a list of type Adjective for each Noun)
 */
public List<Noun> extractSubjects() {
	SemanticGraphHandler graphHandler  = new SemanticGraphHandler(sentenceAnnotation.get(CollapsedCCProcessedDependenciesAnnotation.class));
    List<CoreLabel> tokensList = sentenceAnnotation.get(TokensAnnotation.class);
    List<Noun> nouns = new ArrayList<Noun>();
    for (CoreLabel token : tokensList)
    	if (graphHandler.isSubject(token))
    		nouns.add(createNoun(token));
 	
		return nouns;
	}

private Noun createNoun(CoreLabel token) {
	Noun noun = new Noun();
	noun.setValue(token.lemma());
	List<Adjective> adjs = createAdjectivesFromNoun(token);
	noun.setAdjectives(adjs);
	noun.setPreposition(getNounPrepotition(token));
	return noun;
}

private PartOfSpeech getNounPrepotition(CoreLabel token) {
	SemanticGraphHandler graphHandler  = new SemanticGraphHandler(this.sentenceAnnotation.get(CollapsedCCProcessedDependenciesAnnotation.class));
	PartOfSpeech preposition = null;
	String prepositionValue = graphHandler.getStringPrepotistionFromNoun(token);
	if (prepositionValue!=null&&!prepositionValue.equals(""))
		preposition = new PartOfSpeech(prepositionValue);
	
	return preposition;
}

private List<Adjective> createAdjectivesFromNoun(CoreLabel token) {
	List<Adjective> adjectives = new ArrayList<Adjective>();
	SemanticGraphHandler graphHandler  = new SemanticGraphHandler(this.sentenceAnnotation.get(CollapsedCCProcessedDependenciesAnnotation.class));
    List<CoreLabel> adjectivesTokens = graphHandler.getAdjectivesFromNoun(token);
    for (CoreLabel adj : adjectivesTokens){
    	Adjective adjective = new Adjective();
    	adjective.setValue(adj.lemma());
    	adjectives.add(adjective);
    }
    return adjectives;
}

public List<Verb> extractPredicates() {
	SemanticGraphHandler graphHandler  = new SemanticGraphHandler(sentenceAnnotation.get(CollapsedCCProcessedDependenciesAnnotation.class));
    List<CoreLabel> tokensList = sentenceAnnotation.get(TokensAnnotation.class);
    List<Verb> verbs = new ArrayList<Verb>();
    for (CoreLabel token : tokensList)
    	if (graphHandler.isVerb(token))
    			verbs.add(createVerb(token));
   
	return verbs;
}

private Verb createVerb(CoreLabel token) {
	Verb verb = new Verb();
	verb.setValue(token.lemma());
	List<Adverb> adverbs = createAdverbsfromVerb(token);
	verb.setAdverbs(adverbs);
	return verb;
}

private List<Adverb> createAdverbsfromVerb(CoreLabel token) {
		List<Adverb> adverbs = new ArrayList<Adverb>();
	SemanticGraphHandler graphHandler  = new SemanticGraphHandler(this.sentenceAnnotation.get(CollapsedCCProcessedDependenciesAnnotation.class));
    List<CoreLabel> adverbTokens = graphHandler.getAdverbsFromVerb(token);
    for (CoreLabel adv : adverbTokens){
    	Adverb adverb = new Adverb();
    	adverb.setValue(adv.lemma());
    	adverbs.add(adverb);
    }
    	
	
	return adverbs;
}

public List<Noun> extractObjects() {
	SemanticGraphHandler graphHandler  = new SemanticGraphHandler(sentenceAnnotation.get(CollapsedCCProcessedDependenciesAnnotation.class));
    List<CoreLabel> tokensList = sentenceAnnotation.get(TokensAnnotation.class);
    List<Noun> nouns = new ArrayList<Noun>();
    for (CoreLabel token : tokensList)
    	if (graphHandler.isObject(token))
    		nouns.add(createNoun(token));
 	
		return nouns;
	}

public List<Noun> extractPrepositionObjects() {
	SemanticGraphHandler graphHandler  = new SemanticGraphHandler(sentenceAnnotation.get(CollapsedCCProcessedDependenciesAnnotation.class));
    List<CoreLabel> tokensList = sentenceAnnotation.get(TokensAnnotation.class);
    List<Noun> nouns = new ArrayList<Noun>();
    for (CoreLabel token : tokensList)
    	if (graphHandler.isPrepositionObject(token))
    		nouns.add(createNoun(token));
 	
		return nouns;
	}



}
